package com.itzdm.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.common.basecontext.BaseContext;
import com.itzdm.reggie.dto.OrdersDto;
import com.itzdm.reggie.entity.OrderDetail;
import com.itzdm.reggie.entity.Orders;
import com.itzdm.reggie.service.OrderDetailService;
import com.itzdm.reggie.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     * @param order
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("提交订单")
    private Result<String> submitOrder(@RequestBody Orders order) {
        log.info("订单数据：{}",order);
        orderService.submit(order);
        return Result.success("提交成功");
    }
    @GetMapping("/userPage")
    public Result<Page> page(int page, int pageSize) {
        log.info("分页查询历史订单：{}{}",page,pageSize);
        Page p = orderService.pageOrder(page,pageSize);
        return Result.success(p);
    }


    @GetMapping("/page")
    @ApiOperation("订单明细")
    public Result<Page> OrderPage(int page, int pageSize,Long number,String beginTime,String endTime) {
        log.info("查询订单明细：{}{}{}}{{}}",page,pageSize,number,beginTime,endTime);
        Page p = orderService.OrderPage(page,pageSize,number,beginTime,endTime);
        return Result.success(p);
    }

    @PutMapping
    @ApiOperation("修改订单状态")
    public Result<String> changeStatus(@RequestBody Orders order) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId,order.getId());
        updateWrapper.set(Orders::getStatus,order.getStatus());
        orderService.update(order,updateWrapper);
        return Result.success("修改成功");
    }

    @PostMapping("/again")
    @ApiOperation("再来一单")
    public Result<String> again(@RequestBody Orders order){
        log.info("再来一单：{}",order.getId());
        orderService.again(order);
        return Result.success("喜欢吃就再来一单丫~");
    }
}
